﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace Vigenere.Models
{
    /// <summary>
    /// 将原文和密文封装成一个类
    /// </summary>
    public class MyEncode
    {
        public string PlainInput { get; set; }
        public string KeyInput { get; set; }

    }

    /// <summary>
    /// 加密和输出过程
    /// </summary>
    public class Encode
    {
        /// <summary>
        /// 加密过程（需要已封装好的原文和密文，以及用于存放的数组）
        /// </summary>
        /// <param name="myEncode"></param>
        /// <param name="Key"></param>
        /// <param name="Plain"></param>
        /// <param name="Cipher"></param>
        /// <param name="Ciphertext"></param>
        public string Vigenerefun(MyEncode myEncode, List<int> Key, List<int> Plain, List<int> Cipher, List<char> Ciphertext)
        {
            int length = Key.Count;
            for (int i = 0; i < Plain.Count; i++)
            {
                for (int j = 0; j < Key.Count; j++)
                {
                    if (i % length == j)
                        Cipher.Add((Plain[i] + Key[j]) % 26);                         //对数字进行维吉尼亚加密
                }
            }

            for (int i = 0; i < Plain.Count; i++)
            {
                Ciphertext.Add(Convert.ToChar(Convert.ToInt32('a') + Cipher[i]));     //将加密数字转为加密字符
            }

            return new string(Ciphertext.ToArray());                              //输出密文




        }

        /// <summary>
        /// 将原文和密文转为数字
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="Plain"></param>
        /// <param name="KeyInput"></param>
        /// <param name="PlainInput"></param>
        public void ToNum(List<int> Key, List<int> Plain, string KeyInput, string PlainInput)
        {
            char[] chars = KeyInput.ToArray();
            char[] chars1 = PlainInput.ToArray();
            foreach (var item in chars)
            {
                Key.Add(item - 'a');
            }

            foreach (var item in chars1)
            {
                Plain.Add(item - 'a');
            }

        }
    }


}
